package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.GoodsEvaluateContentGridAdapter;
import com.longcai.piaojia.conn.EvaluateSubmitPost;
import com.longcai.piaojia.conn.GoodsInfoGet;
import com.longcai.piaojia.fragment.FragmentTicketOrder;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.widget.GoodsEvaluateView;
import com.longcai.piaojia.widget.NoScrollGridView;
import com.longcai.piaojia.widget.StartView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class GoodsEvaluateActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.goods_evaluate_gev) private GoodsEvaluateView goods_evaluate_gev;
    @BoundView(R.id.goods_evaluate_nsgv) private NoScrollGridView goods_evaluate_nsgv;
    @BoundView(R.id.goods_evaluate_et) private EditText goods_evaluate_et;
    @BoundView(R.id.goods_info_bottom_submit_tv) private TextView goods_info_bottom_submit_tv;
    @BoundView(R.id.goods_evaluate_iv) private ImageView goods_evaluate_iv;
    @BoundView(R.id.goods_evaluate_title_tv) private TextView goods_evaluate_title_tv;
    private GoodsEvaluateContentGridAdapter goodsEvaluateContentGridAdapter;
    private List<String> lists = new ArrayList<>();
    private int count = 0;
    private GoodsInfoGet.GoodsInfoEntity goodsInfo;
    private EvaluateSubmitPost evaluateSubmitPost = new EvaluateSubmitPost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

//            GoodsEvaluateListActivity.GoodsEvaluateCallBack goodsEvaluateCallBack = (GoodsEvaluateListActivity.GoodsEvaluateCallBack) getAppCallBack(GoodsEvaluateListActivity.class);
//            goodsEvaluateCallBack.refresh();

            MyOrderDetailActivity.RefreshDetail refreshDetail = (MyOrderDetailActivity.RefreshDetail) getAppCallBack(MyOrderDetailActivity.class);
            refreshDetail.afterEvaluateRefresh();

            FragmentTicketOrder.RefreshOrderList refreshOrderList = (FragmentTicketOrder.RefreshOrderList) getAppCallBack(FragmentTicketOrder.class);
            refreshOrderList.refresh();

            MyToast.show(GoodsEvaluateActivity.this, toast, Toast.LENGTH_LONG);

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            MyToast.show(GoodsEvaluateActivity.this, toast, Toast.LENGTH_LONG);


        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        for(int i=0;i<1;i++){

            lists.add("");

        }

        setContentView(R.layout.activity_goods_evaluate);

        goodsInfo = (GoodsInfoGet.GoodsInfoEntity) getIntent().getSerializableExtra("goodsInfo");

        setBackTrue();

        setTitleName(getResources().getString(R.string.goods_evaluate_list_title_text));

        goods_evaluate_title_tv.setText(goodsInfo.title);

        GlideLoader.getInstance().get(this, goodsInfo.picurl, goods_evaluate_iv, R.mipmap.evaluate);

        goods_evaluate_nsgv.setHorizontalSpacing(ScaleScreenHelperFactory.getInstance().getWidthHeight(42));
        goods_evaluate_nsgv.setVerticalSpacing(ScaleScreenHelperFactory.getInstance().getWidthHeight(30));

        goodsEvaluateContentGridAdapter = new GoodsEvaluateContentGridAdapter(this, lists);
        goods_evaluate_nsgv.setAdapter(goodsEvaluateContentGridAdapter);

        goods_evaluate_gev.setOnItemSelectListener(new StartView.OnItemSelectListener() {
            @Override
            public void onItemSelect(int index) {

                count = index;

            }
        });

        goods_info_bottom_submit_tv.setOnClickListener(this);

        goods_evaluate_gev.setSelect(4);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.goods_info_bottom_submit_tv:

                String content = goods_evaluate_et.getText().toString().trim();

                if(TextUtils.isEmpty(content)){

                    MyToast.show(context, getString(R.string.goods_evaluate_please_fill_content_text), Toast.LENGTH_LONG);

                    return;

                } else if (content.length() < 10) {

                    MyToast.show(context, getString(R.string.goods_evaluate_content_hint_text), Toast.LENGTH_LONG);

                    return;
                }


                evaluateSubmitPost.user_id = BaseApplication.BasePreferences.readUserId();
                evaluateSubmitPost.content = content;
                evaluateSubmitPost.show_id = goodsInfo.id;
                evaluateSubmitPost.star = count;
                evaluateSubmitPost.order_number = getIntent().getStringExtra("order_number");
                evaluateSubmitPost.posttime = System.currentTimeMillis()+"";//毫秒时间戳
                Log.e("毫秒时间戳", System.currentTimeMillis()+"");
                evaluateSubmitPost.execute(GoodsEvaluateActivity.this);

                break;

        }

    }
}
